package b3.widget
{
	import b3.scene.*;
	import b3.util.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	public class WidgetManager
	{
		static private var instance:WidgetManager;
		//依次弹出窗口完毕后的回调函数
		private var onOneByOneCallFunction:Function;
		private var onOneByOneParams:Array;
		//同时显示的多个提示信息
		public var severalMessages:Array;
		//仅单独显示的提示信息
		public var mouseOverMessage:Sprite;
		public var oneByOneWidgets:Array;
		public var isOnStage:Boolean;
		
		public function WidgetManager()
		{
			oneByOneWidgets = new Array();
			isOnStage = false;
		}

		static public function getInstance():WidgetManager
		{
			if (!instance)
				instance = new WidgetManager();
			return instance;
		}
		
		//打开鼠标悬浮窗口（互斥） 每次仅有一个弹窗可见
		public function popOutMouseOverMessage(widget:Sprite):void
		{
//			//展示单独窗口时  也要关闭所有提示信息
//			this.closeSeveralMessages();
			closeMouseOverMessage();
			mouseOverMessage = widget;
			SceneManager.getInstance().currentScene.addChild(widget);
		}  
		//关闭鼠标悬浮窗口（互斥）
		public function closeMouseOverMessage():void
		{
			if (mouseOverMessage != null && mouseOverMessage.parent != null)
				mouseOverMessage.parent.removeChild(mouseOverMessage);
			mouseOverMessage = null;
		}
		/**
		 * 参数表示 每次默认打开一个弹窗 逐次增加弹窗并且不删除前面弹窗
		 */ 
		public function popOutSeveralMessages(singlewidget:Sprite):void
		{
			//将新的提示信息添加到原有数组中
			if (singlewidget != null)
			{
				if (severalMessages == null)
				{
					severalMessages = new Array();
				}
				severalMessages.push(singlewidget);
				SceneManager.getInstance().currentScene.addChild(singlewidget);
			}
			
		}
		/**
		 * 同时逐次关闭弹窗
		 */ 
		public function closeSeveralMessages():void
		{
			//如果需要删除多个提示信息
			if (severalMessages != null)
			{
				for (var i:int = 0;i < severalMessages.length;i++)
				{
					var currentWidget:Sprite = severalMessages[i] as Sprite;
					if (currentWidget.parent != null )
					{
						currentWidget.parent.removeChild(currentWidget);
					}
				}
			}
			severalMessages = null;
		}
		
		public function getWidget(DialogClass:Class):DisplayObject
		{
			var scene:BasicScene = SceneManager.getInstance().currentScene;
			for (var i:int = 0; i < scene.numChildren; i++)
			{
				var widget:DisplayObject = scene.getChildAt(i);
				if (widget is DialogClass)
					return widget;
			}
			return null;
		}
		
		public function popOut(widgets:Array, exclusive:Boolean = false):void
		{
			for (var i:int = 0; i < widgets.length; i++)
			{
				var widget:Sprite = widgets[i];
				if (exclusive && i == 0)
					addTransparentBgr(widget);
				SceneManager.getInstance().currentScene.addChild(widget);
			}
			if (exclusive)
				BSound.getInstance().playEffect("SoundButton1");
		}
		
		private function addTransparentBgr(widget:Sprite):void
		{
			widget.addEventListener(Event.REMOVED_FROM_STAGE, onExclusiveWidgetRemoved);
			widget.addEventListener(Event.ADDED_TO_STAGE, onExclusiveWidgetAdded);
		}
		
		private function onExclusiveWidgetRemoved(event:Event):void
		{
			//默认当前被移除的窗口的下一层
			var widget:Sprite = event.currentTarget as Sprite;
			if ( widget != null &&  widget.parent!= null)
				widget.parent.removeChildAt(widget.parent.getChildIndex(widget)-1);
		}

		private function onExclusiveWidgetAdded(event:Event):void
		{
			//此处控制有问题 应该单独设置透明层
			var transparentDebugBgr:Sprite = new Sprite();
			var gfx:Graphics = transparentDebugBgr.graphics;
			gfx.clear();
			gfx.moveTo(0, 0);
			gfx.beginFill(0xFFFFFF, 0.1);
			gfx.lineTo(BFont.stageWidth, 0);
			gfx.lineTo(BFont.stageWidth, BFont.stageHeight);
			gfx.lineTo(0, BFont.stageHeight);
			gfx.lineTo(0, 0);
			gfx.endFill();
			var widget:Sprite = event.currentTarget as Sprite;

			widget.parent.addChildAt(transparentDebugBgr, widget.parent.getChildIndex(widget));
		}
		
		public function setPopOutOneByOneCallBack(onOneByOneCallFunction:Function, onOneByOneParams:Array = null):void
		{
			if (this.onOneByOneCallFunction == null)
			{
				this.onOneByOneCallFunction = onOneByOneCallFunction;
				this.onOneByOneParams = onOneByOneParams;
			}
		}
		
		public function popOutOneByOne(widget:Sprite):void
		{
			oneByOneWidgets.push(widget);
			if (oneByOneWidgets.length == 1 && !isOnStage)
			{
				showNext();
			}
		}
		
		private function showNext(event:Event = null):void
		{
			if (oneByOneWidgets.length == 0)
			{
				isOnStage = false;
				if (onOneByOneCallFunction != null)
				{
					onOneByOneCallFunction.apply(null, onOneByOneParams);
					onOneByOneCallFunction = null;
				}
			}
			else
			{
				var sprite:Sprite = oneByOneWidgets.shift();
				addTransparentBgr(sprite);
				SceneManager.getInstance().currentScene.addChild(sprite);
				isOnStage = true;
				sprite.addEventListener(Event.REMOVED_FROM_STAGE, showNext);
			}
		}
		
	}
}